﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace AndrewTicTacToe
{
    public partial class Form1 : Form
    {
        internal GameServer server { get; set; }

        public Form1()
        {
           
            InitializeComponent();
            
        }

        //Method which is called when the form is loaded
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "X";
            //sendData("Button1isTrue");
            //receiveData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Text = "X";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Text = "X";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4.Text = "X";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button5.Text = "X";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            button6.Text = "X";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            button7.Text = "X";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            button8.Text = "X";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            button9.Text = "X";
        }

      //  private void sendData(string buttonState)
      //  {
      //   //   private static void SendResponse(TcpClient client, string message)
      ////make sure the other end decodes with the same format!
      //     byte[] bytes = Encoding.Unicode.GetBytes(buttonState);
      //     server.clients.First().client.GetStream().Write(bytes, 0, bytes.Length);


      //  }

        //private void receiveData()
        //{
        //  string receivedString;
        //  byte[] buffer = new byte[256];
        //  int totalRead = 0;

        //  //read bytes until stream indicates there are no more
        //  do
        //  {
        //      //blocks until client sends message:	
        //      int read = server.clients.First().client.GetStream().Read
        //          (buffer, totalRead, buffer.Length - totalRead);
        //      totalRead += read;
        //  } while (server.clients.First().client.GetStream().DataAvailable);

        //  receivedString = Encoding.Unicode.GetString(buffer, 0, totalRead);


        //    if (receivedString == "button1isTrue")
        //        button1.Text = "X";
        //}
        private void startAGameServerToolStripMenuItem_Click(object sender, EventArgs e)
        {   //Start Game Server
            String blah;
            blah = Microsoft.VisualBasic.Interaction.InputBox("Something ??", "enter text here I gues??");
            System.Diagnostics.Debug.WriteLine("blah string:" + blah);
            
            label1.Text = "Status: Waiting for gamers...";
            // Initialize Server sequences:
            server = new GameServer();
            label1.Text = "Status: Connected!";

        }

        //private void connectAsClientToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    String blah;
        //    blah = Microsoft.VisualBasic.Interaction.InputBox("Something ??", "enter text here I gues??");
        //    System.Diagnostics.Debug.WriteLine("blah string:" + blah);

        //    label1.Text = "Waiting for gamers...";
        //    // Initialize Server sequences:
        //    //server = new Server();
        //    label1.Text = "Connected!";
        //}

        private void connectToGameServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Client making an attempt to connect to Server
            TcpClient clientSocket = clientSocket = new TcpClient();
            StreamWriter streamSender;

            try
            {
                String IP = Microsoft.VisualBasic.Interaction.InputBox("Input IP Adresss of Server ??", "enter text here I gues??");
                clientSocket.Connect(IP, 1300);
                streamSender = new StreamWriter(clientSocket.GetStream());
                //createButtons();
                label1.Text = "Connected!";
            }
            catch (Exception)
            {
                MessageBox.Show("Client: Server is offline", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
